package com.ncst.backend.controller;

import com.ncst.backend.common.Result;
import com.ncst.backend.common.PageResult;
import com.ncst.backend.domain.Register;
import com.ncst.backend.domain.vo.RegisterPage;
import com.ncst.backend.service.RegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 挂号控制器
 */
@Api(tags = "挂号管理")
@Slf4j
@RestController
@RequestMapping("/registers")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @ApiOperation("根据ID查询挂号记录")
    @GetMapping("/id/{id}")
    public Result<Register> getById(@ApiParam("挂号ID") @PathVariable Integer id) {
        Register register = registerService.getById(id);
        return register != null ? Result.success(register) : Result.notFound("挂号记录不存在");
    }

    @ApiOperation("查询所有挂号记录")
    @GetMapping
    public Result<List<Register>> getAll() {
        return Result.success(registerService.getAll());
    }

    @ApiOperation("根据条件查询挂号记录")
    @PostMapping("/search")
    public Result<List<Register>> getByCondition(@ApiParam("查询条件") @RequestBody Register register) {
        return Result.success(registerService.getByCondition(register));
    }

    @ApiOperation("分页查询挂号记录")
    @GetMapping("/page")
    public Result<PageResult<RegisterPage>> getByPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("病历号") @RequestParam(required = false) String caseNumber,
            @ApiParam("姓名") @RequestParam(required = false) String realName,
            @ApiParam("看诊状态") @RequestParam(required = false) Integer visitState,
            @ApiParam("部门ID") @RequestParam(required = false) Integer deptmentId,
            @ApiParam("开始日期") @RequestParam(required = false) String startDate,
            @ApiParam("结束日期") @RequestParam(required = false) String endDate) {
        Register register = new Register();
        register.setCaseNumber(caseNumber);
        register.setRealName(realName);
        register.setVisitState(visitState);
        register.setDeptmentId(deptmentId);
        // 将日期参数作为查询条件传递给服务层，返回带 total 的分页结果
        return Result.success(registerService.page(pageNum, pageSize, register, startDate, endDate));
    }

    @ApiOperation("统计挂号记录总数")
    @GetMapping("/count")
    public Result<Long> countAll() {
        return Result.success(registerService.countAll());
    }

    @ApiOperation("根据条件统计挂号数量")
    @PostMapping("/count")
    public Result<Long> countByCondition(@ApiParam("查询条件") @RequestBody Register register) {
        return Result.success(registerService.countByCondition(register));
    }

    @ApiOperation("新增挂号记录")
    @PostMapping
    public Result<Void> save(@ApiParam("挂号信息") @RequestBody Register register) {
        return registerService.save(register) ? Result.success("新增成功") : Result.error("新增失败");
    }

    @ApiOperation("批量新增挂号记录")
    @PostMapping("/batch")
    public Result<Void> saveBatch(@ApiParam("挂号列表") @RequestBody List<Register> list) {
        return registerService.saveBatch(list) ? Result.success("批量新增成功") : Result.error("批量新增失败");
    }

    @ApiOperation("根据ID更新挂号记录")
    @PutMapping("/{id}")
    public Result<Void> updateById(@ApiParam("挂号ID") @PathVariable Integer id, @ApiParam("挂号信息") @RequestBody Register register) {
        register.setId(id);
        return registerService.updateById(register) ? Result.success("更新成功") : Result.error("更新失败");
    }

    @ApiOperation("根据ID删除挂号记录/退号")
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@ApiParam("挂号ID") @PathVariable Integer id) {
        return registerService.removeById(id) ? Result.success("删除成功") : Result.error("删除失败");
    }

    @ApiOperation("根据病历号或姓名查询患者信息")
    @PostMapping("/patient/search")
    public Result<Register> getPatientByCaseNumberOrName(@RequestBody CaseNumberAndName params) {
        Register patient = registerService.getByCaseNumberOrRealName(params.getCaseNumber(), params.getRealName());
        return patient != null ? Result.success(patient) : Result.notFound("未找到患者信息");
    }

    @Data
    static class CaseNumberAndName {
        private String caseNumber;
        private String realName;
    }

    @ApiOperation("批量删除挂号记录")
    @PostMapping("/batch-refund")
    public Result<Void> removeByIds(@ApiParam("挂号ID列表") @RequestBody List<Integer> ids) {
        return registerService.removeByIds(ids) ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }
}
